package com.kuangjia.student_ems.dto;

import com.kuangjia.student_ems.entity.User;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDate;
import java.time.LocalDateTime;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class UserDTO {
    
    private Long id;
    private String username;
    private String name;
    private String email;
    private String phone;
    private User.UserRole role;
    private Long departmentId;
    private String departmentName;
    private Long positionId;
    private String positionName;
    private User.EmployeeStatus status;
    private LocalDate entryDate;
    private LocalDate leaveDate;
    private String leaveReason;
    private LocalDateTime createTime;
    private LocalDateTime updateTime;
    
    public static UserDTO fromEntity(User user) {
        UserDTO dto = new UserDTO();
        dto.setId(user.getId());
        dto.setUsername(user.getUsername());
        dto.setName(user.getName());
        dto.setEmail(user.getEmail());
        dto.setPhone(user.getPhone());
        dto.setRole(user.getRole());
        dto.setStatus(user.getStatus());
        dto.setEntryDate(user.getEntryDate());
        dto.setLeaveDate(user.getLeaveDate());
        dto.setLeaveReason(user.getLeaveReason());
        dto.setCreateTime(user.getCreateTime());
        dto.setUpdateTime(user.getUpdateTime());
        
        if (user.getDepartment() != null) {
            if(user.getDepartment().getId() != null) dto.setDepartmentId(user.getDepartment().getId());
            if(user.getDepartment().getName() != null) dto.setDepartmentName(user.getDepartment().getName());
        } else {
            dto.setDepartmentId(null);
            dto.setDepartmentName(null);
        }
        
        if (user.getPosition() != null) {
            if(user.getPosition().getId() != null) dto.setPositionId(user.getPosition().getId());
            if(user.getPosition().getName() != null) dto.setPositionName(user.getPosition().getName());
        } else {
            dto.setPositionId(null);
            dto.setPositionName(null);
        }
        
        return dto;
    }

    public String getDepartmentName() {
        return departmentName;
    }

    public void setDepartmentName(String departmentName) {
        this.departmentName = departmentName;
    }
} 